//
//  NewFeatureViewController.swift
//  weibo
//
//  Created by apple on 15/12/16.
//  Copyright © 2015年 LYR. All rights reserved.
//

import UIKit
import SnapKit

private let reuseIdentifier = "Cell"
private let MaxImageCount = 4

class NewFeatureViewController: UICollectionViewController {

    init() {
    
//        设置layout  流式 布局
        let layout = UICollectionViewFlowLayout()
        
//        设置cell的大小
        layout.itemSize = UIScreen.mainScreen().bounds.size
        
//        设置cell滑动的方向
        layout.scrollDirection = .Horizontal
        
//        设置cell的左右间隔
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
//        调用super
        super.init(collectionViewLayout: layout)
    
//        设置分页效果
        collectionView?.pagingEnabled = true
        
//        隐藏滚动条
        collectionView?.showsHorizontalScrollIndicator = false
        
//        取消弹簧效果
        collectionView?.bounces = false
    
    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Register cell classes  注意去缓冲池重用的时候要修改成自定义的cell
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
    }

    
    // MARK: UICollectionViewDataSource  数据源方法

//    此方法不实现的时候默认返回 1
//    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
//        // #warning Incomplete implementation, return the number of sections
//        return 0
//    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        
        return MaxImageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
    
//        给index 赋值
        cell.index = indexPath.item
        
//        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.redColor() : UIColor.blueColor()
    
        return cell
    }
    
    //   按钮的动画效果需要在cell全部显示出来之后再执行，下面的方法就是每一个cell显示完之后会执行的方法
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        // 上一个cell的index
//        print("\(indexPath.item)")
        
        //获取准确的cell
        let cell = collectionView.visibleCells().last as! NewFeatureCell
        
        //获取cell的indexpath
        let index = collectionView.indexPathForCell(cell)!
    
        if  index.item == MaxImageCount - 1 {
        
        cell.startAnimation()
            
        }
        
    }
    
    
    
    
}

//    自定义cell
class NewFeatureCell: UICollectionViewCell {
    
//    定义index属性
    var index: Int = 0 {
    
        didSet {
        iconView.image = UIImage(named: "new_feature_\(index+1)")
//        print("\(index+1)")
            
//            动画之前隐藏按钮
            startBtn.hidden = true
        
        }
    
    }
    
//    重写初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    设置UI  添加子视图
    private func setupUI() {
    
//        添加背景图片  和 开始按钮
        contentView.addSubview(iconView)
        contentView.addSubview(startBtn)
        
//      对子控件进行布局约束
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView)
        }
        startBtn.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(contentView.snp_centerX)
            
            make.bottom.equalTo(contentView.snp_bottom).offset(-160)
        }
        
//        添加点击事件
        startBtn.addTarget(self, action: "startBtnClick", forControlEvents: .TouchUpInside)
        
    
    }
    
//    添加动画效果
    func startAnimation() {
    
//        将之前隐藏的动画显示出来
        startBtn.hidden = false
        
//        设置初始形变
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 7, options: [], animations: { () -> Void in
        
        self.startBtn.transform = CGAffineTransformIdentity
        
        }) { (_) -> Void in
            
            print("新特性页面展示完毕")
        }

    }
    
//    按钮的点击事件
    @objc private func startBtnClick() {
    
    print("开始体验被点击了。。。。")
        
        // 发送通知 切换视图
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVCNotification, object: nil)
    
    }
    
    
//   懒加载所有的子视图
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    private lazy var startBtn: UIButton = {
    
      let btn = UIButton()
        btn.setTitle("开始体验", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(16)
        
        return btn
    }()
    
    
}









